<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2017/12/7
 * Time: 下午2:55
 */

namespace XmtApp\Modules\Admin\Controllers\Api;


use App\Http\Requests\ArticlePost;
use App\Http\Resources\ArticleResource;
use App\Http\Resources\ArticlesResource;
use App\Tables\Article;
use Carbon\Carbon;
use Illuminate\Http\Request;
use XmtApp\Modules\Admin\Controllers\Controller;
use Storage;
use Illuminate\Pagination\LengthAwarePaginator;

class ArticleController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 文章列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $table = new Article();

        $table = $table->withTrashed();

        if ($request->has('page')) {
            $data = $table->paginate($request->header('pageSize'));

            $_data = $data->each(function ($item) {
                $item->openness_text = $item->openness_text;
                $item->category_text = $item->category_text;
                $item->tags_text = $item->tags_text;
                $item->status_text = $item->status_text;
                $item->is_delete = $item->trashed();
            });
            $data = new LengthAwarePaginator($_data, $data->total(), $data->perPage(), $data->currentPage());
        } else {
            $data = $table->get();
            $data->each(function ($item) {
                $item->openness_text = $item->openness_text;
                $item->category_text = $item->category_text;
                $item->tags_text = $item->tags_text;
                $item->status_text = $item->status_text;
                $item->is_delete = $item->trashed();
            });
        }

        return new ArticlesResource($data);
    }

    /**
     * 保存文章
     * @param ArticlePost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(ArticlePost $request)
    {
        $data = $request->all([
            'title', 'sub_title', 'content', 'openness',
            'status', 'published_at', 'category_id', 'tag_ids', 'has_image',
            'is_top'
        ]);

        $article = new Article($data);
        $article->has_image = $article->hasImage();

        if (empty($article->published_at)) {
            $article->published_at = Carbon::now();
        }

        $res = $article->save();
        if ($res) {
            return response()->json(ajax_return(true, '保存成功', $article->toArray()));
        } else {
            return response()->json(ajax_return(false, '保存失败'));
        }
    }

    /**
     * 更新文章
     * @param $id
     * @param ArticlePost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update($id, ArticlePost $request)
    {
        $data = $request->all([
            'title', 'sub_title', 'content', 'openness',
            'status', 'published_at', 'category_id', 'tag_ids',
            'is_top'
        ]);

        $article = new Article();

        if (empty($data['published_at'])) {
            $data['published_at'] = Carbon::now();
        }

        if (!empty($data['tag_ids'])) {
            $data['tag_ids'] = json_encode($data['tag_ids']);
        }

        if (!in_array($article->status, ['draft', 'wait_audit'])) {
            $data['status'] = 'wait_audit';
        }

        $res = $article->where('id', $id)->update($data);
        if ($res) {
            return response()->json(ajax_return(true, '文章更新成功', new ArticleResource($article)));
        } else {
            return response()->json(ajax_return(false, '文章更新失败'));
        }
    }

    /**
     * 文章审核-拒绝通过
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function reject($id)
    {
        $article = Article::where('id', $id)->first();
        if (empty($article)) {
            return response()->json(ajax_return(false, '文章信息不存在'));
        }
        $article->status = 'rejection';

        $res = $article->save();
        if ($res) {
            return response()->json(ajax_return(true, '文章已被拒绝通过', new ArticleResource($article)));
        } else {
            return response()->json(ajax_return(false, '文章审核失败'));
        }
    }

    /**
     * 文章已通过审核
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function pass($id)
    {
        $article = Article::where('id', $id)->first();
        if (empty($article)) {
            return response()->json(ajax_return(false, '文章信息不存在'));
        }

        if ($article->published_at->lt(Carbon::now())) {
            $article->published_at = Carbon::now();
        }

        $article->status = 'published';

        $res = $article->save();
        if ($res) {
            return response()->json(ajax_return(true, '文章已通过审核', new ArticleResource($article)));
        } else {
            return response()->json(ajax_return(false, '文章审核失败'));
        }
    }

    /**
     * 获取文章详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $article = Article::find($id);

        if (empty($article)) {
            return response()->json(ajax_return(false, '获取文章信息失败'));
        } else {
            return response()->json(ajax_return(true, 'OK', new ArticleResource($article)));
        }
    }

    /**
     * 删除文章
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        $res = Article::destroy($id);
        if ($res) {
            return response()->json(ajax_return(true, '删除成功'));
        } else {
            return response()->json(ajax_return(false, '删除失败'));
        }
    }

    /**
     * 文章恢复
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function restore($id)
    {
        $res = Article::where('id', $id)->restore();
        if ($res) {
            return response()->json(ajax_return(true, '恢复成功'));
        } else {
            return response()->json(ajax_return(false, '恢复失败'));
        }
    }

    /**
     * 文章图片上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadImage(Request $request)
    {
        $path = $request->file('file')->store(date('Y/m/d'), 'article');
        $url = Storage::disk('article')->url($path);

        return response()->json(['location' => $url]);
    }
}